﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;

namespace Sale_Operation
{
    public class SupplierOp : Component
    {
        public override bool Save(object obj)
        {
            bool blRes = false;
            Supplier SM = obj as Supplier;
           
            try
            {
                StringBuilder sbSql = new StringBuilder();
                if (SM.Supp_Id == 0)
                {
                    sbSql.Append("INSERT INTO Supplier (Supp_Code,Supp_Name,Supp_LinkMan1,Supp_Tel1,Supp_LinkMan2,Supp_Tel2,Supp_Memo,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("Supp_Code='" + ValueHandler.GetStringValue(SM.Supp_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_Name='" + ValueHandler.GetStringValue(SM.Supp_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_LinkMan1='" + ValueHandler.GetStringValue(SM.Supp_LinkMan1, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_Tel1='" + ValueHandler.GetStringValue(SM.Supp_Tel1, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_LinkMan2='" + ValueHandler.GetStringValue(SM.Supp_LinkMan2, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_Tel2='" + ValueHandler.GetStringValue(SM.Supp_Tel2, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_Memo='" + ValueHandler.GetStringValue(SM.Supp_Memo, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State=1,");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.Supp_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "新增供应商信息[" + ValueHandler.GetStringValue(SM.Supp_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.供应商管理);

                }
                else
                {
                    sbSql.Append("UPDATE Supplier SET ");
                    sbSql.Append("Supp_Code='" + ValueHandler.GetStringValue(SM.Supp_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_Name='" + ValueHandler.GetStringValue(SM.Supp_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_LinkMan1='" + ValueHandler.GetStringValue(SM.Supp_LinkMan1, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_Tel1='" + ValueHandler.GetStringValue(SM.Supp_Tel1, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_LinkMan2='" + ValueHandler.GetStringValue(SM.Supp_LinkMan2, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_Tel2='" + ValueHandler.GetStringValue(SM.Supp_Tel2, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("Supp_Memo='" + ValueHandler.GetStringValue(SM.Supp_Memo, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE Supp_Id=" + SM.Supp_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "修改供应商信息[" + ValueHandler.GetStringValue(SM.Supp_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.供应商管理);
                }
                blRes = true;

            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改供应商信息出错！[" + ex.ToString() + "]", EMenuList.供应商管理);
            }
            db.Close();
            return blRes;
        }
        public override bool HasExists(object obj)
        {
            Supplier SM = obj as Supplier;
            bool blRes = true;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM Supplier WHERE Supp_Code='" + SM.Supp_Code + "' AND Supp_Id<>" + SM.Supp_Id + " AND State=1 ");
                sbSql.Append(" UNION ALL ");
                sbSql.Append(" SELECT * FROM Supplier WHERE Supp_Name='" + SM.Supp_Name + "' AND Supp_Id<>" + SM.Supp_Id + " AND State=1 ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
 
        }
        public override bool Delete(object obj)
        {
            Supplier SM = obj as Supplier;
          
            bool blRes = false;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append("UPDATE Supplier SET ");
                sbSql.Append("State=0");
                sbSql.Append("WHERE Supp_Id=" + SM.Supp_Id);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "删除供应商信息[" + ValueHandler.GetStringValue(SM.Supp_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.供应商管理);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "删除供应商信息出错！[" + ex.ToString() + "]", EMenuList.供应商管理);
            }
            db.Close();
            return blRes;
 
        }

        public override DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT Supp_Id,Supp_Code,Supp_Name,Supp_LinkMan1,Supp_Tel1,Supp_LinkMan2,Supp_Tel2,Supp_Memo,State,CreateTime,CreateMan FROM Supplier WHERE State=1 ");                
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY CreateTime DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "供应商列表加载失败！[" + ex.ToString() + "]", EMenuList.供应商管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public override DataTable GetList(string strCondition)
        {
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT Supp_Id,Supp_Code,Supp_Name,Supp_LinkMan1,Supp_Tel1,Supp_LinkMan2,Supp_Tel2,Supp_Memo,State,CreateTime,CreateMan FROM Supplier WHERE State=1 ");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY CreateTime DESC ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "供应商列表加载失败！[" + ex.ToString() + "]", EMenuList.供应商管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public override object GetModel(object obj)
        {
            Supplier SM = obj as Supplier;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT Supp_Id,Supp_Code,Supp_Name,Supp_LinkMan1,Supp_Tel1,Supp_LinkMan2,Supp_Tel2,Supp_Memo,State,CreateTime,CreateMan FROM Supplier WHERE State=1 AND Supp_Id=" + SM.Supp_Id);
                 
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
                if (db.Data.Tables.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = System.DateTime.Parse(dr["CreateTime"].ToString());
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                    SM.Supp_Code = ValueHandler.GetStringValue(dr["Supp_Code"]);
                    SM.Supp_LinkMan1 = ValueHandler.GetStringValue(dr["Supp_LinkMan1"]);
                    SM.Supp_LinkMan2 = ValueHandler.GetStringValue(dr["Supp_LinkMan2"]);
                    SM.Supp_Memo = ValueHandler.GetStringValue(dr["Supp_Memo"]);
                    SM.Supp_Name = ValueHandler.GetStringValue(dr["Supp_Name"]);
                    SM.Supp_Tel1 = ValueHandler.GetStringValue(dr["Supp_Tel1"]);
                    SM.Supp_Tel2 = ValueHandler.GetStringValue(dr["Supp_Tel2"]);
                }
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "供应商列表加载失败！[" + ex.ToString() + "]", EMenuList.供应商管理);
            }
            db.Close();
            return SM;
        }
    }
}
